home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / diff_2_1.lha / diff-2.1 / getopt1.c < prev    next >
C/C++ Source or Header  |  1993-02-03  |  4KB  |  164 lines

  1. /* Getopt for GNU.
  2.    Copyright (C) 1987, 88, 89, 90, 91, 1992 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify it
  5.    under the terms of the GNU General Public License as published by the
  6.    Free Software Foundation; either version 2, or (at your option) any
  7.    later version.
  8.    
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.    
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. #ifdef HAVE_CONFIG_H
  19. #include "config.h"
  20. #endif
  21.  
  22. #include "getopt.h"
  23.  
  24. #ifndef AMIGA
  25. #if !__STDC__ && !defined(const) && IN_GCC
  26. #define const
  27. #endif
  28. #endif /* !AMIGA */
  29.  
  30. #include <stdio.h>
  31.  
  32. /* This needs to come after some library #include
  33.    to get __GNU_LIBRARY__ defined.  */
  34. #ifdef __GNU_LIBRARY__
  35. #include <stdlib.h>
  36. #else
  37. char *getenv ();
  38. #endif
  39.  
  40. #ifndef    NULL
  41. #define NULL 0
  42. #endif
  43.  
  44. int
  45. getopt_long (argc, argv, options, long_options, opt_index)
  46.      int argc;
  47.      char *const *argv;
  48.      const char *options;
  49.      const struct option *long_options;
  50.      int *opt_index;
  51. {
  52.   return _getopt_internal (argc, argv, options, long_options, opt_index, 0);
  53. }
  54.  
  55. /* Like getopt_long, but '-' as well as '--' can indicate a long option.
  56.    If an option that starts with '-' (not '--') doesn't match a long option,
  57.    but does match a short option, it is parsed as a short option
  58.    instead.  */
  59.  
  60. int 
  61. getopt_long_only (argc, argv, options, long_options, opt_index)
  62.      int argc;
  63.      char *const *argv;
  64.      const char *options;
  65.      const struct option *long_options;
  66.      int *opt_index;
  67. {
  68.   return _getopt_internal (argc, argv, options, long_options, opt_index, 1);
  69. }
  70.  
  71. #ifdef TEST
  72.  
  73. #include <stdio.h>
  74.  
  75. int
  76. main (argc, argv)
  77.      int argc;
  78.      char **argv;
  79. {
  80.   int c;
  81.   int digit_optind = 0;
  82.  
  83.   while (1)
  84.     {
  85.       int this_option_optind = optind ? optind : 1;
  86.       int option_index = 0;
  87.       static struct option long_options[] =
  88.       {
  89.     {"add", 1, 0, 0},
  90.     {"append", 0, 0, 0},
  91.     {"delete", 1, 0, 0},
  92.     {"verbose", 0, 0, 0},
  93.     {"create", 0, 0, 0},
  94.     {"file", 1, 0, 0},
  95.     {0, 0, 0, 0}
  96.       };
  97.  
  98.       c = getopt_long (argc, argv, "abc:d:0123456789",
  99.                long_options, &option_index);
  100.       if (c == EOF)
  101.     break;
  102.  
  103.       switch (c)
  104.     {
  105.     case 0:
  106.       printf ("option %s", long_options[option_index].name);
  107.       if (optarg)
  108.         printf (" with arg %s", optarg);
  109.       printf ("\n");
  110.       break;
  111.  
  112.     case '0':
  113.     case '1':
  114.     case '2':
  115.     case '3':
  116.     case '4':
  117.     case '5':
  118.     case '6':
  119.     case '7':
  120.     case '8':
  121.     case '9':
  122.       if (digit_optind != 0 && digit_optind != this_option_optind)
  123.         printf ("digits occur in two different argv-elements.\n");
  124.       digit_optind = this_option_optind;
  125.       printf ("option %c\n", c);
  126.       break;
  127.  
  128.     case 'a':
  129.       printf ("option a\n");
  130.       break;
  131.  
  132.     case 'b':
  133.       printf ("option b\n");
  134.       break;
  135.  
  136.     case 'c':
  137.       printf ("option c with value `%s'\n", optarg);
  138.       break;
  139.  
  140.     case 'd':
  141.       printf ("option d with value `%s'\n", optarg);
  142.       break;
  143.  
  144.     case '?':
  145.       break;
  146.  
  147.     default:
  148.       printf ("?? getopt returned character code 0%o ??\n", c);
  149.     }
  150.     }
  151.  
  152.   if (optind < argc)
  153.     {
  154.       printf ("non-option ARGV-elements: ");
  155.       while (optind < argc)
  156.     printf ("%s ", argv[optind++]);
  157.       printf ("\n");
  158.     }
  159.  
  160.   exit (0);
  161. }
  162.  
  163. #endif /* TEST */
  164.